<?php

App::uses('AdminController', 'Controller');

class AdminSlideshowController extends AdminController {
	
	public $uses = array("Slideshow");
	
	public function index() {
		$this->set("slideshow", $this->Slideshow->find('all'));
	}
	
	public function save() {
		if($this->request->is('post')) {
			// fix bug with Slideshow key moved from 1st array item
			if(isset($this->data['Slideshow'])) {
				$data = $this->data;
				$data[0]['Slideshow'] = array_merge($data[0]['Slideshow'], $data['Slideshow']);
				unset($data['Slideshow']);
				$this->data = $data;
			}
			
			$this->Slideshow->truncate();
			$save = true;
			foreach($this->data as $item) {
				$this->Slideshow->create();
				
				unset($item['Slideshow']['id']);
				$this->Slideshow->set($item);
				$save = $save && $this->Slideshow->save();
			}
			if($save) {
				$this->setFlash('Izmene su začuvane.', 'ok');
				return $this->redirect('/admin_slideshow');
			}else{
				$this->setFlash('Izmene nisu začuvane.');
				$this->set("slideshow", $this->data);
				$this->render('index');
			}
		}else{
			$this->redirect("index");
		}
	}
}
